package com.coocaa.x.app.framework.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;

import com.coocaa.x.framework.app.CoocaaActivity;
import com.coocaa.x.framework.app.UpgradeManager;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.liteupgrade.aidl.UpgradeParams;
import com.skyworth.framework.skysdk.properties.SkyGeneralProperties;

import java.util.Stack;

public abstract class LiteAppActivity extends CoocaaActivity {

    private static Stack<String> activityStack = new Stack<String>();

    protected static int getActivityStackSize() {
        synchronized (activityStack) {
            return activityStack.size();
        }
    }

    private static boolean bCheckUpgrade = false;

    protected void handleUpgrade() {
        post(new Runnable() {
            @Override
            public void run() {
                if (!bCheckUpgrade) {
                    bCheckUpgrade = true;

                    UpgradeParams params = new UpgradeParams();
                    params.params.put("device_channel", LiteAppApplication.getDeviceChannel());
                    params.params.put("show_dialog", "only_has_update");
                    UpgradeManager.checkUpgrade(params);
                }
            }
        });
    }

    private static final String QUICK_RESUME_ACTION = "com.skyworth.broadcast.standby.quick.suspend";

    private BroadcastReceiver quickResumeReceiver = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        synchronized (activityStack) {
            activityStack.add(this.toString());
        }
        if (quickResumeReceiver == null) {
            quickResumeReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(final Context context, final Intent intent) {
                    if (intent.getAction().equals(QUICK_RESUME_ACTION)) {
                        LogUtils.d("quickResumeReceiver", "sunny shut:" + intent.getAction());
//                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                }
            };

            IntentFilter filter = new IntentFilter();
            filter.addAction(QUICK_RESUME_ACTION);
            registerReceiver(quickResumeReceiver, filter);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        post(new Runnable() {
            @Override
            public void run() {
                UpgradeManager.exec("enable_dialog", null);
                handleUpgrade();
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        post(new Runnable() {
            @Override
            public void run() {
                UpgradeManager.exec("disable_dialog", null);
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        post(new Runnable() {
            @Override
            public void run() {
                UpgradeManager.exec("disable_dialog", null);
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (quickResumeReceiver != null) {
            unregisterReceiver(quickResumeReceiver);
            quickResumeReceiver = null;
        }
        synchronized (activityStack) {
            activityStack.remove(toString());
            if (!SkyGeneralProperties.getBoolProperty("HIGH_MEMORY")) {
                if (activityStack.size() == 0) {
                    android.os.Process.killProcess(android.os.Process.myPid());
                    System.exit(0);
                }
            }
        }
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            synchronized (activityStack) {
                if (activityStack.size() == 1) {
                    post(new Runnable() {
                        @Override
                        public void run() {
                            UpgradeManager.exec("disable_dialog", null);
                        }
                    });
                    new Runnable() {
                        @Override
                        public void run() {
                            onActivityStackExit();
                            //System.exit(0);
                        }
                    }.run();
                }
            }
        }

        return super.onKeyDown(keyCode, event);
    }

    protected void onActivityStackExit() {

    }
}
